package com.erp.admin.business.controller;
import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.base.base.BaseController;
import com.erp.base.base.Page;
import com.erp.base.business.entity.Region;
import com.erp.base.business.service.RegionService;
import com.erp.base.common.utils.R;
/**
 * 
 * 
 * @author whw
 * @email whongwei_yz@sina.com
 * @date 2018-12-04 13:35:21
 */
 
@Controller
@RequestMapping("/business/region")
public class RegionController extends BaseController{
	@Autowired
	private RegionService regionService;
	
	@GetMapping()
	@RequiresPermissions("business:region:region")
	String Region(){
	    return "business/region/region";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("business:Region:region")
	public Page<Region> list(Region region,HttpServletRequest request,HttpServletResponse response){
		//查询列表数据
		Page<Region> page= Page.bootstrapPage(request, response);
		page = regionService.findPage(page, region);
		return page;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("business:region:add")
	String add(){
	    return "business/region/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("business:region:edit")
	String edit(@PathVariable("id") Long id,Model model){
		Region region = regionService.get(id);
		model.addAttribute("region", region);
	    return "business/region/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("business:region:add")
	public R save( Region region,Model model){
		if(!beanValidator(model, region)){
			return R.error((String)model.asMap().get("message"));
		}
		try {
				regionService.save(region);
				return R.ok();
			} catch (Exception e) {
				logger.error("保存失败", e);
				return R.error();
			}
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("business:region:edit")
	public R update( Region region,Model model){
		if(!beanValidator(model, region)){
			return R.error((String)model.asMap().get("message"));
		}
		try {
				regionService.save(region);
				return R.ok();
			} catch (Exception e) {
				logger.error("保存失败", e);
				return R.error();
			}
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("business:region:remove")
	public R remove( Long id){
		try {
			regionService.delete(id);
			return R.ok();
		} catch (Exception e) {
			logger.error("删除失败， id:"+ id, e);
			return R.error();
		}
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("business:region:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		try {
			regionService.deleteBatch(Arrays.asList(ids));
			return R.ok();
		} catch (Exception e) {
			logger.error("删除失败，ids:"+ids, e);
			return R.error();
		}

	}
	
}
